home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / awe2-0_1.lha / awe2-0.1 / Src / Semaphore.cc < prev    next >
C/C++ Source or Header  |  1990-07-09  |  3KB  |  151 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. // 
  3. // Copyright (C) 1988 University of Illinois, Urbana, Illinois
  4. // Copyright (C) 1989 University of Colorado, Boulder, Colorado
  5. // Copyright (C) 1990 University of Colorado, Boulder, Colorado
  6. //
  7. // written by Dirk Grunwald (grunwald@foobar.colorado.edu)
  8. //
  9. #include "CpuMultiplexor.h"
  10. #include "CpuMultiplexorP.h"
  11. #include "Semaphore.h"
  12. #include "FifoScheduler.h"
  13. #include "Thread.h"
  14.  
  15. Semaphore::Semaphore(int count, ThreadContainer *scheduler)
  16. {
  17.     if (scheduler == 0) {
  18.     pScheduler = new FifoScheduler;
  19.     iDidAlloc = 1;
  20.     } else {
  21.     pScheduler = scheduler;
  22.     iDidAlloc = 0;
  23.     }
  24.     pCount = count;
  25. };
  26.  
  27. Semaphore::~Semaphore()
  28. {
  29.     lock.reserve();
  30.  
  31.     if (pScheduler != 0) {
  32.     if (! pScheduler  -> isEmpty() ) {
  33.         cerr << "[Semaphore] Attempted to delete non-empty semaphore\n";
  34.         exit(1);
  35.     }
  36.     if (iDidAlloc) delete pScheduler;
  37.     pScheduler = 0;
  38.     iDidAlloc = 0;
  39.     }
  40.  
  41.     lock.release();
  42. }
  43.  
  44. void Semaphore::reserve()
  45. {
  46.     lock.reserve();
  47.     if (pCount < 1) {
  48.     lock.release();
  49.     ThisCpu -> reserveByException( this );
  50.     }
  51.     else {
  52.     pCount--;
  53.     lock.release();
  54.     }
  55. }
  56.  
  57. //
  58. //    This is only executed in the context of a CpuMultiplexor, never
  59. //    a thread.
  60. //
  61. int
  62. Semaphore::reserveByException(Thread *byWho)
  63. {
  64.     bool blocked = 0;
  65.     lock.reserve();
  66.     if (pCount < 1) {
  67.     pScheduler -> add( byWho );
  68.     blocked = 1;
  69.     }
  70.     pCount--;
  71.     lock.release();
  72.     return( blocked );
  73. }
  74.  
  75. bool Semaphore::reserveNoBlock()
  76. {
  77.     bool gotIt = 1;
  78.  
  79.     lock.reserve();
  80.     if (pCount < 1) {
  81.     gotIt = 0;
  82.     }
  83.     else {
  84.     pCount--;
  85.     }
  86.     lock.release();
  87.     return( gotIt );
  88. }
  89.     
  90. void Semaphore::release()
  91. {
  92.     Thread *p = 0;
  93.     lock.reserve();
  94.     if (pCount < 0) {
  95.     assert( !pScheduler -> isEmpty() );
  96.     p = pScheduler -> remove();
  97.     }
  98.     pCount++;
  99.     lock.release();
  100.  
  101.     if (p != 0) {
  102.     ThisCpu -> add(p);
  103.     }
  104. }
  105.  
  106. //
  107. //    Need to lock these
  108. //
  109.  
  110. unsigned Semaphore::size()
  111. {
  112.     return(pScheduler -> size());
  113. }
  114.  
  115. int Semaphore::count(int xcount)
  116. {
  117.     lock.reserve();
  118.     int tmp = pCount;
  119.     pCount = xcount;
  120.     lock.release();
  121.     return(tmp);
  122. }
  123.  
  124. int Semaphore::count()
  125. {
  126.     lock.reserve();
  127.     int tmp = pCount;
  128.     lock.release();
  129.     return(tmp);
  130. }
  131.  
  132. void Semaphore::incrCount(int increment)
  133. {
  134.     lock.reserve();
  135.     pCount += increment;
  136.     lock.release();
  137. }
  138.  
  139. #ifdef UNDEF
  140. void Semaphore::classPrintOn(ostream& out)
  141. {
  142.     int size = pScheduler -> size();
  143.  
  144.     out << " Semaphore with pCount =" << pCount << " and "
  145.     << size << " enqueued tasks\n";
  146.     if (size > 0) {
  147.     out << "Tasks are:\n" << *pScheduler << "\n";
  148.     }
  149. }
  150. #endif
  151.